$(function() {
	module('tagging');
	
	test('Generate Add Menu', 3, function() {
		var addMenu = {Story: {tags:["Agile_Story", {jsonPath:"$.doc._id"}]} };
		var result = {
			doc : {_id:'123'}
		};
		var menu = wikid.generateAddMenu(addMenu, result);  
		equals(menu.name,'Story');
		equals(menu.tags[0],'Agile_Story');
		equals(menu.tags[1],'123');
	});
	
	test('Compound Path String', 4, function() {
		var result = {
			doc : {_id:'123'}
		};
		var string_test = "Test1";
		equals(wikid.compoundPathString(string_test), string_test);
		equals(wikid.compoundPathString(string_test, result), string_test);
		
		var obj_test = {jsonPath : "$.doc._id"};
		equals(wikid.compoundPathString(obj_test, result), "123");
		
		var arr_test = ["http://thesite.com/report?", {jsonPath : "$.doc._id"}];
		equals(wikid.compoundPathString(arr_test, result), "http://thesite.com/report?123");
	
	});
	test('Inheritence Add menu', 4, function() {
		var story = {
			_id : "Project",
			"-instance" : {
				addmenu : {Story: {tags:["Agile_Story", {jsonPath:"$.doc._id"}]} },
			}
		};
		var result_rows = [{key:"Project", value:story}]
		var db_mock = createViewMock({"system/tag_to_instanceInfo" : result_rows });
		
		
		var project = {
		  _id : "SAMPLEPROJ",
		  "-tags" : ["Project"],
		  Project : {  
			greenbridgeReportPrefix : "http://place.com/report.html?" 
		  }
		}
		var result = wikid.toWikid(project, db_mock); 
		equals(result.menu.add.length, 1);
		equals(result.menu.add[0].name, "Story");
		equals(result.menu.add[0].tags[0], "Agile_Story");
		equals(result.menu.add[0].tags[1], "SAMPLEPROJ");
	
	});
	
	
	/** 
	 * We need to rethink how we do the tools menu. This works, but the Agile_Story becomes very tightly
	 * coupled to the Project instance SAMPLEPROJ .
	 */
	test("Tools Menu Entry - Cross tags", 3, function() {
		var story = {
			_id : "Agile_Story",
			"-instance" : {
				toolsmenu : {'Greenbridge Report':  [{jsonPath:"$.instanceInfos.SAMPLEPROJ.Project.greenbridgeReportPrefix"},{jsonPath:"$.doc._id"}]} ,
			}
		};
		var project = {
		  _id : "SAMPLEPROJ",
		  "-tags" : ["Project"],
		  Project : {  
			greenbridgeReportPrefix : "http://place.com/report.html?" 
		  }
		}
		var result_rows = [{key:"Agile_Story", value:story}, {key:"SAMPLEPROJ",value:project}]
		var db_mock = createViewMock({"system/tag_to_instanceInfo" : result_rows });
		
		var story = {
			_id : "SAMPLEPROJ_7",
			"-tags" : ["Agile_Story", "SAMPLEPROJ"]
		}
		
		var result = wikid.toWikid(story, db_mock); 
		equals(result.menu.tools.length, 1);
		equals(result.menu.tools[0].name, "Greenbridge Report");
		equals(result.menu.tools[0].url, "http://place.com/report.html?SAMPLEPROJ_7");
	
	});
	
	/** A better way for now, but every thing in the project gets a link! */
	test("Tools Menu Entry", 3, function() {
		var project = {
		  _id : "SAMPLEPROJ",
		  "-tags" : ["Project"],
		  "-instance" : {
				toolsmenu : {'Greenbridge Report':  [{jsonPath:"$.instanceInfos.SAMPLEPROJ.Project.greenbridgeReportPrefix"},{jsonPath:"$.doc._id"}]} ,
		  },
		  Project : {  
			greenbridgeReportPrefix : "http://place.com/report.html?" 
		  }
		}
		var result_rows = [{key:"SAMPLEPROJ",value:project}]
		var db_mock = createViewMock({"system/tag_to_instanceInfo" : result_rows });
		
		var story = {
			_id : "SAMPLEPROJ_7",
			"-tags" : ["SAMPLEPROJ"]
		}
		
		var result = wikid.toWikid(story, db_mock); 
		equals(result.menu.tools.length, 1);
		equals(result.menu.tools[0].name, "Greenbridge Report");
		equals(result.menu.tools[0].url, "http://place.com/report.html?SAMPLEPROJ_7");
	
	});
	asyncTest("Instance Loading Script", 1, function() {
		var project = {
		  _id : "Project",
		  "-instance" : {
				script : true		
		  }
		}
		var result_rows = [{key:"Project",value:project}]
		var db_mock = createViewMock({"system/tag_to_instanceInfo" : result_rows });
		
		var story = {
			_id : "SAMPLEPROJ",
			"-tags" : ["Project"]
		}
		
		var result;
		// the mock Project.js file calls the "tag.Project" event, and alters the result
		// adding some temp data as an example.
		$(window).bind("tag.Project", function() {
			start();
			equals(result.temp, 'Some data!');
		});		
		
		result = wikid.toWikid(story, db_mock); 
		stop(5000);
	});
});